// 预定义
var NEW_TMP_NAME = "newpart";
var MODIFY_TMP_NAME = "modifypart";
var oldName = "";

//页面载入初始化
$( "body" ).ready( function( e )
{
	var table = $( "#department" );
	bindClickFunc( table );
} );

// 为所有部门结构点添加点击方法
function bindClickFunc( table )
{
	var trs = table.find( "tr[class!=fields]" );
	var pkey = "";

	trs.each( function( id )
	{
		// 绑定修改当前部门名称的方法
		$( this ).find( "td > a:not(a:last)" ).bind( "click", nodeClick );

		// 绑定添加新部门的方法
		$( this ).find( "td > a:last" ).bind( "click", function()
		{
			pkey = $( this ).parent().attr( "id" );
			addNewPart( pkey );
		} );
	} );
}

// 检测是否已经有正在处理的增加或者修改操作
// 增加：直接移除
// 修改：还原名称
function checkInputNode()
{
	// 增加
	$( "#ipt_" + NEW_TMP_NAME ).parent().remove();

	// 修改
	var a = $( "#ipt_" + MODIFY_TMP_NAME ).parent();
	a.html( oldName );

	// 恢复点击事件绑定
	a.bind( "click", nodeClick );
}

function nodeClick()
{
	// 检测是否当前已有正在操作的节点
	checkInputNode();

	pkey = $( this ).parent().attr( "id" );
	var key = $( this ).attr( "id" );
	oldName = $( this ).text();

	$( this ).html(
			"<input id='ipt_" + MODIFY_TMP_NAME + "' rel='" + key + "' type='text' size='10' value='"
					+ oldName + "'/>" + "<input type='button' id='btn_ok'/>"
					+ "<input type='button' id='btn_cancel'/>" );

	// 临时取消点击事件，以免重复点击会删除名称
	$( this ).unbind( "click" );

	$( "#ipt_" + MODIFY_TMP_NAME ).focus();
	$( "#ipt_" + MODIFY_TMP_NAME ).bind( "keyup", function( e )
	{
		if( e.which == 13 )
			postUpdatePart();
	} );
	$( "#btn_ok" ).bind( "click", postUpdatePart );
	$( "#btn_cancel" ).bind( "click", delNowPart );
}

function delNowPart()
{
	var name = $( "#ipt_" + MODIFY_TMP_NAME ).val();
	var key = $( "#ipt_" + MODIFY_TMP_NAME ).attr( "rel" );

	submitter.post( contextPath+"/index/addDepartment.json", { type: "del", name: name, key: key }, function( d )
	{
		if( d.err == 0 )
		{
			// 删除节点
			$( "#" + d.key ).remove();

			if( d.type == "部门" )
			{
				// 如果删除的是部门，则还要删除部门下的所有职位列表（添加部门同理）
				// 删除相关列表
				$( "#fid_" + d.key ).parent().remove();
				$( "#lst_" + d.key ).parent().remove();
			}

			// 提示删除成功
			alert("删除成功");
			window.location.href=contextPath+"/index/struct_department.htm";
		}
		else
		{
			alert("删除成功")
			window.location.href=contextPath+"/index/struct_department.htm";
		}
	}, "json" );
}

// 添加一个新的部门结点
function addNewPart( pkey )
{
	// 检测是否当前已有正在操作的节点
	checkInputNode();

	// parent 为空则添加部门，否则添加职位
	if( pkey == "" || pkey == undefined )
		pkey = "root";

	// td parent
	var td = $( "#" + pkey );

	// 删除可能存在的上次未完成的临时添加节点
	$( "#" + NEW_TMP_NAME ).remove();

	// 添加新节点
	td.find( ".add" ).before(
			"<a id='" + NEW_TMP_NAME + "'><input id='ipt_" + NEW_TMP_NAME + "' rel='" + pkey
					+ "' type='text' size='10' value='" + ( pkey == "lst_root" ? "新部门" : "新职位" ) + "'/>"
					+ "<input type='button' id='btn_ok'/>" + "<input type='button' id='btn_cancel'/>"
					+ "</a>" );

	$( "#ipt_" + NEW_TMP_NAME ).focus();
	$( "#btn_ok" ).bind( "click", postNewPart );
	$( "#ipt_" + NEW_TMP_NAME ).bind( "keyup", function( e )
	{
		if( e.which == 13 )
			postNewPart();
	} );
	$( "#btn_cancel" ).bind( "click", function()
	{
		$( this ).parent().remove();
	} );
}

function postNewPart()
{
	var name = $( "#ipt_" + NEW_TMP_NAME ).val();
	var pkey = $( "#ipt_" + NEW_TMP_NAME ).attr( "rel" );
	pkey = pkey.substr( 4 );

	if( name == "" )
	{
		infopad.show( "名称不能为空。" );
		return;
	}

	submitter.post( contextPath+"/index/addDepartment.json", { type: "add", name: name, pkey: pkey }, function( d )
	{
		if( d.err == 0 )
		{
			$( "#" + NEW_TMP_NAME ).replaceWith( "<a id='" + d.key + "' title='点击修改'>" + d.name + "</a>" );
			$( "#" + d.key ).bind( "click", nodeClick );

			if( d.type == "部门" )
			{
				$( "#department" ).append(
						"<tr class=\"fields\"><th id=\"fid_" + d.key + "\">" + d.name
								+ "</th></tr><tr><td id=\"lst_" + d.key
								+ "\"><a class=\"add\" title=\"添加新职位\">+</a></td></tr>" );

				// 绑定添加新节点的方法
				$( "#lst_" + d.key ).find( "a:last" ).bind( "click", function()
				{
					addNewPart( "lst_" + d.key );
				} );
			}

			alert("添加成功")
			window.location.href=contextPath+"/index/struct_department.htm";
		}
		else
		{
			alert("添加成功")
			window.location.href=contextPath+"/index/struct_department.htm";
		}
	}, "json" );
}

function postUpdatePart()
{
	var name = $( "#ipt_" + MODIFY_TMP_NAME ).val();
	var key = $( "#ipt_" + MODIFY_TMP_NAME ).attr( "rel" );

	if( name == "" )
	{
		infopad.show( "名称不能为空。" );
		return;
	}

	submitter.post( contextPath+"/index/addDepartment.json", { type: "modify", name: name, key: key }, function( d )
	{
		if( d.err == 0 )
		{
			$( "#" + d.key ).html( d.name );
			$( "#fid_" + d.key ).html( d.name );
			infopad.show( d.msg );

			// 恢复点击事件绑定
			$( "#" + d.key ).bind( "click", nodeClick );
			window.location.href=contextPath+"/index/struct_department.htm";
		}
		else
		{
			alert("修改成功");
			window.location.href=contextPath+"/index/struct_department.htm";
		}
	}, "json" );
}
